package com.productinfo.service.impl;

import com.area.entity.Brand;
import com.area.entity.Producttype;
import com.area.mapper.BrandMapper;
import com.area.mapper.ProducttypeMapper;
import com.code.util.Variable;
import com.productinfo.entity.Productinfo;
import com.productinfo.entity.Vo.ProductInfoVo;
import com.productinfo.mapper.ProductinfoMapper;
import com.productinfo.service.IProductinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2023-02-10
 */
@Service
@Slf4j
public class ProductinfoServiceImpl extends ServiceImpl<ProductinfoMapper, Productinfo> implements IProductinfoService {

    @Resource
    private ProductinfoMapper productinfoMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Autowired(required = false)
    private BrandMapper brandMapper;

    @Autowired(required = false)
    private ProducttypeMapper producttypeMapper;

    @Value("${mall.productinfoByUrl}")
    private String productinfoByUrl;

    /**
     * 查询所有商品
     *
     * @return
     */
    @Override
    public List<ProductInfoVo> queryProductInfoAll() {
        //查看缓存中是否已经存在数据
        List<Productinfo> productinfos = (List<Productinfo>) redisTemplate.opsForValue().get(Variable.PRODUCT_INFO_LIST);
        if (productinfos == null) {
            productinfos = productinfoMapper.queryProductInfoAll();
        }
        return getListObjToListObjVo(productinfos);
    }

    /**
     * 根据主键查询
     *
     * @param productId
     * @return
     */
    @Override
    public ProductInfoVo queryProductInfoById(String productId) {
        Productinfo productinfo = productinfoMapper.queryProductInfoById(productId);
        return getObjToObjVo(productinfo);
    }

    /**
     * 根据分类id 查询关联商品
     *
     * @param productId
     * @return
     */
    @Override
    public List<ProductInfoVo> queryRelevantCommodity(String productId) {
        List<Productinfo> productInfos = productinfoMapper.queryRelevantCommodity(productId);
        return getListObjToListObjVo(productInfos);
    }


    /**
     * 工具类
     * Obj To ObjVo
     *
     * @param productinfo
     * @return
     */
    private ProductInfoVo getObjToObjVo(Productinfo productinfo) {
        ProductInfoVo productInfoVo = new ProductInfoVo();
        //复制到新对象
        BeanUtils.copyProperties(productinfo, productInfoVo);
        //进行新内容的补充
        productInfoVo.setAvatar(productinfo.getLogoImg());
        productInfoVo.setContent(productinfo.getProductFullName());
        productInfoVo.setDescription("价格：" + productinfo.getSalesPrice().toString() + " 元");
        productInfoVo.setHref(productinfo.getLogoImg());
        productInfoVo.setTitle(productinfo.getProductName());
        Brand brand = brandMapper.queryBrandId(productinfo.getBrandId());
        productInfoVo.setBrandName(brand.getBrandName());
        Producttype producttype = producttypeMapper.queryProductTypeId(productinfo.getProductTypeId());
        productInfoVo.setProductTypeName(producttype.getSortName());
        productInfoVo.setProductTypeCode(producttype.getSortCode());
        productInfoVo.setProductInfoByUrl(productinfoByUrl + productinfo.getProductId());
        return productInfoVo;
    }

    /**
     * 工具类
     * List<Obj> To List<ObjVo>
     *
     * @param productinfos
     * @return
     */
    private List<ProductInfoVo> getListObjToListObjVo(List<Productinfo> productinfos) {
        //进行二次处理
        List<ProductInfoVo> productInfoVos = new ArrayList<>();
        for (Productinfo productinfo : productinfos) {
            //创建对象
            ProductInfoVo productInfoVo = new ProductInfoVo();
            //复制到新对象
            BeanUtils.copyProperties(productinfo, productInfoVo);
            //进行新内容的补充
            productInfoVo.setAvatar(productinfo.getLogoImg());
            productInfoVo.setContent(productinfo.getProductFullName());
            productInfoVo.setDescription("价格：" + productinfo.getSalesPrice().toString() + " 元");
            productInfoVo.setHref(productinfo.getLogoImg());
            productInfoVo.setTitle(productinfo.getProductName());
            Brand brand = brandMapper.queryBrandId(productinfo.getBrandId());
            productInfoVo.setBrandName(brand.getBrandName());
            Producttype producttype = producttypeMapper.queryProductTypeId(productinfo.getProductTypeId());
            productInfoVo.setProductTypeName(producttype.getSortName());
            productInfoVo.setProductTypeCode(producttype.getSortCode());
            productInfoVo.setProductInfoByUrl(productinfoByUrl + productinfo.getProductId());
            //写入到新的集合中
            productInfoVos.add(productInfoVo);
        }
        return productInfoVos;
    }

}







